<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <title>渠道列表</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="/bower_components/Ionicons/css/ionicons.min.css">
  <!-- DataTables -->
  <link rel="stylesheet" href="/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="/dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!--头部-->
  <header id="headerDiv" class="main-header">
    <!--#include  file="/pages/admin/header.html"-->
  </header>

  <!--左侧边栏 -->
  <aside id="sidebarDiv" class="main-sidebar">
    <!--#include  file="/pages/admin/sidebar.html"-->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <small>渠道列表</small>
      </h1>
      <ol class="breadcrumb">
        <li><i class="fa fa-group"></i> 渠道管理</li>
        <li><a href="javascript:void(0);">渠道列表</a></li>
      </ol>

    </section>

    <div class="container">
      <div class="row clearfix">
        <div class="col-md-12 column">
          <div class="modal fade" id="modal-container-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h4 class="modal-title" id="myModalLabel">
                    新增
                  </h4>
                </div>
                <div class="box box-primary">

                  <!-- /.box-header -->
                  <div class="box-body">
                    <form id="dataForm" role="form">
                      <!-- text input -->
                      <input type="hidden" id="aid"/>
                      <div class="form-group">
                        <label>用户名</label>
                        <input id="username" name="username" type="text" class="form-control" placeholder="用户名">
                      </div>
                      <div class="form-group">
                        <label>密码</label>
                        <input id="password" name="password" type="text" class="form-control" placeholder="密码">
                      </div>
                      <div class="form-group">
                        <label>联系人</label>
                        <input id="realname" type="text" class="form-control" placeholder="联系人">
                      </div>
                      <div class="form-group">
                        <label>QQ</label>
                        <input id="qq" type="text" class="form-control" placeholder="QQ">
                      </div>
                      <div class="form-group">
                        <label>客服列表</label>
                        <select id="service" class="form-control">
                        </select>
                      </div>
                    </form>
                  </div>
                  <!-- /.box-body -->
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> <button id="save" type="button" class="btn btn-primary">保存</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Main content -->
    <section class="content">

      <!-- /.row -->
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header">
              <form role="form" class="form-inline pull-left">
                <button id="modal-1" href="#modal-container-1" role="button"  data-toggle="modal" type="button" class="btn btn-success"><i class="fa fa-group"></i>&nbsp;&nbsp;<span class="bold">新增渠道</span></button>
              </form>
              <form role="form" class="form-inline pull-right">
                <label class="control-label">搜索:</label>
                <div class="form-group">
                  <select id="stats_search" class="form-control m-b" name="account">
                    <option value="">状态</option>
                    <option value="0">正常</option>
                    <option value="1">锁定</option>
                  </select>
                  <select id="type" class="form-control m-b" name="account">
                    <option value="id">ID</option>
                    <option value="name">名称</option>
                    <option value="recommend">下线查询</option>
                  </select>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <input id="searchinput" type="text" class="form-control">
                    <span class="input-group-btn">
                      <button id="search" type="button" class="btn btn-primary "><i class="fa fa-search"></i></button>
                    </span>
                  </div>
                </div>
              </form>
            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive">
              <table id="datatable" class="table table-bordered table-hover table-striped">
                <thead>
                <tr>
                  <th style=" white-space:nowrap">ID</th>
                  <th style=" white-space:nowrap">名称</th>
                  <th style=" white-space:nowrap">总收益</th>
                  <th style=" white-space:nowrap">未结算收益</th>
                  <th style=" white-space:nowrap">下线人数</th>
                  <th style=" white-space:nowrap">上线ID</th>
                  <th style=" white-space:nowrap">客服</th>
                  <th style=" white-space:nowrap">状态</th>
                  <th style=" white-space:nowrap">操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
    </section>
    <!-- /.content -->
  </div>

  <!-- /.content-wrapper -->
  <!--底部-->
  <footer id="footerDiv" class="main-footer">
    <!--#include  file="/pages/admin/footer.html"-->
  </footer>

  <!-- Control Sidebar -->
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
<!-- jQuery 3 -->
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/morris.js/morris.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="/bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="/dist/js/adminlte.min.js"></script>
<script src="/dist/js/store.min.js"></script>
<script src="/dist/js/layer/layer.js"></script>
<script src="/dist/js/bootstrapValidator.min.js"></script>


<script>
    var datatable;
    let uid = getUrlParam("uid");
    if(uid != null && uid!==''){
        $("#searchinput").val(uid);
    }
    $(function () {

        $('#dataForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                username: {
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                }
            }
        });
        loadService();
        datatable = $("#datatable").DataTable({
            // 'scrollX' : $(document).width(),
            'aLengthMenu' : [20],
            'scrollCollapse' : true,//
            'stateSave' : false,//保存配置状态
            'processing' : false, // 显示loading
            'paging' : true,
            'lengthChange' : false,
            'searching' : false,// 取消搜索框
            'ordering' : false,// 取消字段排序
            'info' : true,
            'autoWidth' : false,
            'deferRender': true,
            'serverSide' : true, // ajax请求时必须写本项
            // 'pagingType' : 'full_numbers',
            'ajax' : { // ajax请求
                'type' : "POST",
                'url' : "/lmgame/webmaster/page",
                'data' : function (d) {
                    let recommend = store.get("recommend");
                    if(recommend != null){
                        store.remove("recommend");
                        d.recommend = recommend;
                        $("#type").val("recommend");
                        $("#searchinput").val(recommend);
                    }else{
                        let type = $("#type").val();
                        if(type == 'id'){
                            d.id = $("#searchinput").val();
                        }else{
                            d.username = $("#searchinput").val();
                        }
                        d.stats = $("#stats_search").val();
                    }
                    return d;
                },
                "error": function(xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    if(xhr.status === 400){
                        window.location.href = "/pages/admin/login.html";
                    }
                    if(xhr.status === 403){
                        window.location.href = "/403.html";
                    }
                }
            },
            'columns' : [ // data数据绑定对应列
                {"data" : null},
                {"data" : "username"},
                {"data" : "money"},
                {"data" : "week_money"},
                {"data" : "count"},
                {"data" : "recommend"},
                {"data" : "service_name"},
                {"data" : "stats"},
                {
                    "class":          'notab',
                    "data":           null
                }
                // {"data" : "siteId"}
            ],
            "columnDefs" : [

                {
                    targets: 0,
                    render: function (a) {
                        return '<a href="javascript:jumpwm('+a.id+',\''+a.username+'\')">'+a.id+'</a>';
                    }
                },
                {
                    targets: 7,
                    render: function (a) {
                        if(a === '0'){
                            return "正常";
                        }
                        if(a === '1'){
                            return "<span style='color:red;'>锁定</span>";
                        }
                    }
                },
                {
                    targets: 8,
                    render: function (a) {
                        let html = "";
                        if(a.stats === '0'){
                            html += "<button onclick='updateStats("+a.id+",1)' type=\"button\" class=\"btn btn-primary btn-sm\">锁定</button>&nbsp;";
                        }else{
                            html += "<button onclick='updateStats("+a.id+",0)' type=\"button\" class=\"btn btn-primary btn-sm\">激活</button>&nbsp;";
                        }
                        html += "<button onclick='editer("+a.id+")' type=\"button\" class=\"btn btn-default btn-sm\">编辑</button>&nbsp;";
                        html += "<button onclick='deleteConfirm("+a.id+")' type=\"button\" class=\"btn btn-danger btn-sm\">删除</button>&nbsp;&nbsp;";
                        html += "<button onclick='sucai("+a.id+")' type=\"button\" class=\"btn btn-info btn-sm\">素材管理</button>";

                        return html;
                    }
                }
            ],
            'language' : {
                'lengthMenu' : "显示 _MENU_ 项结果", // 每页显示多少条
                'zeroRecords' : "没有记录！", // 没有记录时显示的内容
                'info' : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项", // 当前页信息
                'infoEmpty' : "显示第 0 至 0 项结果，共 0 项", // 当前页信息-没有记录时
                'paginate' : {
                    'previous' : " << ",
                    'next' : " >> "
                }
            }
        });

        $("#search").click(function () {
            datatable.ajax.reload(false);
        });

        $("#save").click(function () {
            if($("#dataForm").data("bootstrapValidator").isValid()) {
                let username = $("#username").val();
                let password = $("#password").val();
                let realname = $("#realname").val();
                let qq = $("#qq").val();
                let service = $("#service").val();
                let url = "/lmgame/webmaster/addUser";
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {
                        "username": username,
                        "password": password,
                        "realname": realname,
                        "qq": qq,
                        "service":service
                    },
                    dataType: "json",
                    success: function (data) {

                        if (data.code != 0) {
                           // divfloat(data.msg, 210, 30);
                            layer.msg(data.msg);
                            return;
                        } else {
                            layer.msg('新增成功', {
                                icon: 1,
                                time: 1000 //3秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                $("#modal-container-1").modal("hide");
                                datatable.ajax.reload(false);
                            });

                        }

                    },
                    error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                        if (xhr.status === 400) {
                            window.location.href = "/pages/admin/login.html";
                        }
                        if (xhr.status === 403) {
                            window.location.href = "/403.html";
                        }
                    }
                });
            }
        });
    });

    // 确认删除
    function deleteConfirm(id) {
        layer.confirm('确定删除该渠道吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            let url="/lmgame/webmaster/delById";
            $.ajax({
                type : 'POST',
                url : url,
                data :{"id":id,},
                dataType : "json",
                success: function(data) {
                    if (data.code != 0) {
                        layer.msg(data.msg);
                    } else {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000 //3秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            datatable.ajax.reload(false);
                        });
                    }
                },
                error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                    flag=true;
                    if (xhr.status === 400) {
                        window.location.href = "/pages/admin/login.html";
                    }
                    if (xhr.status === 403) {
                        window.location.href = "/403.html";
                    }
                }
            });
        }, function(){

        });

    }

    // 确认修改
    function updateStats(id, stats){
        let url="/lmgame/webmaster/updateStats";
        $.ajax({
            type : 'POST',
            url : url,
            data : {
                "id":id,
                "stats":stats
            },
            dataType : "json",
            success: function(data) {
                if (data.code != 0) {
                    layer.msg(data.msg);
                } else {
                    let msg='操作成功';
                    if(stats=="1"){
                        msg='锁定成功';
                    }else{
                        msg='激活成功';
                    }
                    layer.msg(msg, {
                        icon: 1,
                        time: 1000 //3秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        datatable.ajax.reload(false);
                    });
                }
            },
            error: function(xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    function editer(id) {
        store.set("id",id);
        window.location.href = "/pages/webmaster/editer.html"
    }

    function jumpwm(id, name) {
        var newWin = window.open();
        $.ajax({
            type: 'POST',
            url: "/lmgame/webmaster/jumpwm",
            data: {
                "sid": id,
                "wmname": name
            },
            dataType: "json",
            success: function (data) {
                let userinfo = store.get("user_info");
                userinfo.sid = id;
                userinfo.wmname = name;
                store.set("user_info", userinfo);
                newWin.location.href = "/pages/channel/home.html";
            },
            error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    function loadService() {
        let url = "/lmgame/admin/csList";
        $.ajax({
            type: 'POST',
            url: url,
            data: {},
            dataType: "json",
            success: function (data) {
                let html ="";
                data.data.forEach(function(key){
                    html+="<option value="+key.id+">"+key.username+"</option>";
                });
                $("#service").html(html);
            },
            error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    //素材管理
    function sucai(id) {
        store.set("uid",id);
        window.location.href = "/pages/webmaster/sucaiList.html"
    }

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r!=null) return unescape(r[2]); return null; //返回参数值
    }

</script>

</body>
</html>
